package com.skt.controller;


import com.skt.domain.DTO.CostumeQueryDTO;
import com.skt.domain.VO.CostumeDetailVO;
import com.skt.domain.VO.CostumeVO;
import com.skt.exception.*;
import com.skt.result.PageResult;
import com.skt.service.CostumeService;
import com.skt.utils.LogUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequiredArgsConstructor
@RequestMapping("/costumes")
@Tag(name = "服装展示接口")
public class CostumeController {

    private final CostumeService costumeService;

    @Operation(
            summary = "分页查询服装列表",
            description = "支持多维度筛选、排序和搜索功能的分页查询。支持按分类、价格区间、商家等条件筛选，支持按价格、销量、上新时间等排序。"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功，返回分页的服装列表"),
            @ApiResponse(responseCode = "400", description = "请求参数错误，如分页参数不合法"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "500", description = "服务器内部处理错误")
    })
    @GetMapping
    public ResponseEntity<PageResult<CostumeVO>> getCostumeList(@Valid CostumeQueryDTO queryParams) {
        // 设置追踪ID
        LogUtil.setTraceId();
        LogUtil.startTimer("COSTUME_LIST_QUERY");

        try {
            PageResult<CostumeVO> result = costumeService.getCostumeList(queryParams);
            return ResponseEntity.ok(result);
        } catch (BusinessException e) {
            LogUtil.error("COSTUME", "LIST_QUERY", "服装列表查询业务异常", e);
            throw e;
        } catch (Exception e) {
            LogUtil.error("COSTUME", "LIST_QUERY", "服装列表查询系统异常", e);
            throw CostumeException.of(CostumeErrorCode.QUERY_COSTUME_LIST_FAILED,"分页查询服装列表失败");
        } finally {
            LogUtil.stopTimer();
            LogUtil.clearContext();
        }
    }

    @Operation(
            summary = "获取服装详情",
            description = "根据服装ID获取服装的详细信息，包括基本信息、规格参数、库存状态等。"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功，返回服装详细信息"),
            @ApiResponse(responseCode = "400", description = "请求参数错误，如ID格式不合法"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "404", description = "服装不存在或已被删除"),
            @ApiResponse(responseCode = "500", description = "服务器内部处理错误")
    })
    @GetMapping("/{costumeId}")
    public ResponseEntity<CostumeDetailVO> getCostumeDetail(
            @Parameter(description = "服装ID", required = true, example = "123456")
            @PathVariable Long costumeId) {

        // 设置追踪ID
        LogUtil.setTraceId();
        LogUtil.startTimer("COSTUME_DETAIL_QUERY");

        try {
            CostumeDetailVO result = costumeService.getCostumeDetail(costumeId);
            return ResponseEntity.ok(result);
        } catch (BusinessException e) {
            LogUtil.error("COSTUME", "DETAIL_QUERY", "服装详情查询业务异常，服装ID：" + costumeId, e);
            throw e;
        } catch (Exception e) {
            LogUtil.error("COSTUME", "DETAIL_QUERY", "服装详情查询系统异常，服装ID：" + costumeId, e);
            throw CostumeException.of(CostumeErrorCode.QUERY_COSTUME_DETAIL_FAILED, "查询服装详情失败");
        } finally {
            LogUtil.stopTimer();
            LogUtil.clearContext();
        }
    }

    @Operation(
            summary = "搜索服装商品",
            description = "根据关键词搜索服装商品，支持分页、多维度筛选和排序。支持按商品名称、描述、标签等进行模糊搜索。"
    )
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "搜索成功，返回分页的服装搜索结果"),
            @ApiResponse(responseCode = "400", description = "请求参数错误，如搜索关键词为空或分页参数不合法"),
            @ApiResponse(responseCode = "401", description = "未授权访问"),
            @ApiResponse(responseCode = "500", description = "服务器内部处理错误")
    })
    @GetMapping("/search")
    public ResponseEntity<PageResult<CostumeVO>> searchCostumes(@Valid CostumeQueryDTO queryParams) {
        // 设置追踪ID
        LogUtil.setTraceId();
        LogUtil.startTimer("COSTUME_SEARCH_QUERY");

        try {
            // 所有业务逻辑判断都移到服务层
            PageResult<CostumeVO> result = costumeService.searchCostumes(queryParams);
            return ResponseEntity.ok(result);
        } catch (BusinessException e) {
            LogUtil.error("COSTUME", "SEARCH_QUERY", "服装搜索业务异常，关键词：" + queryParams.getKeyword(), e);
            throw e;
        } catch (Exception e) {
            LogUtil.error("COSTUME", "SEARCH_QUERY", "服装搜索系统异常，关键词：" + queryParams.getKeyword(), e);
            throw CostumeException.of(CostumeErrorCode.SEARCH_COSTUME_FAILED, "搜索服装商品失败");
        } finally {
            LogUtil.stopTimer();
            LogUtil.clearContext();
        }
    }
}
